﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


public static class Globals
{
    public static class Network
    {
        public static Boolean DebugMode = true;
        public static String ServerIp = "46.163.109.112";
        public static Int32 Port = 1337;
    }

    public static class Separator
    {
        public static String Message = "|#|";
        public static String Content = "|-|";
    }

    public static class Security
    {
        public static String PublicKey = "B393E109CG47F";
        public static String PrivateKey = "N46UG1JHC791D";

        public static String MySql_UserName = "RemoteAdmin";
        public static String MySql_Password = "6dZEuGZPZ6LcDyemn8CSvKX7";
        public static String MySql_Port = "3307";
        public static String MySql_Schema = "gpc";

    }

    public static class Tables
    {
        public static string USER = "Users";
        public static string SESSION = "Sessions";
        public static string CUSERSESSION = "conUserSession";
        public static string FRIENDREQUESTS = "vbfriendrequests";
        public static string CONTACTS = "vbuseruser";
    }

    public static class UserFields
    {
        public static string ID = "userid";
        public static string NAME = "username";
        public static string X = "posx";
        public static string Y = "posy";
        public static string IP = "userip";
        public static string PORT = "userport";
        //TODO umbenennen in was anderes...zu offensichtlich innerhalb der DB :D
        public static string PASSWORD = "userpassword";
        public static string LASTLOGIN = "userlastlogin";
        public static string EMAIL = "userEMail";

    }
    public static class SessionFields
    {
        public static string ID = "sessionid";
        public static string DATE = "date";
        public static string TIME = "time";
    }
    public static class ConRequestFields
    {
        public static string ID = "field_id";
        public static string REQUESTER = "requesterID";
        public static string TARGET = "targetID";
        public static string DECLINED = "blndeclined";
    }
    public static class Contacts
    {
        public static string ID = "field_id";
        public static string USERA = "userida";
        public static string USERB = "useridb";
    }
    public static class ConUserSessionFields
    {
        public static string ID = "connectionID";
        public static string USERID = "fuserid";
        public static string SESSIONID = "fsessionid";
    }
    public static class ConnectionSettings
    {
        public static int PORT = 1337;

    }
    
    /// <summary>
    /// check's if all required tables exist and are correctly configured.
    /// if a required table doesn't exists, it will be created.
    /// if a required table isn't correctly configured, it will be configured.
    /// </summary>
    /// <param name="xConnection"></param>
    /// <returns>false, if something went extremely wrong.</returns>
    internal static Boolean checkDataBase(MySqlConnection xConnection)
    {
        Boolean _return;
        _return = true;
        if (!checkUserTable(xConnection)) _return = false; ;
        if (!checkSessionTable(xConnection)) _return = false;
        if (!checkConUsersSessionsTable(xConnection)) _return = false;

        return _return;
    }

    #region Database Check
    /// <summary>
    /// Checks if the required user tabel exists
    /// </summary>
    /// <param name="xConnection"></param>
    /// <returns>true, if the user table exists</returns>
    private static Boolean checkUserTable(MySqlConnection xConnection)
    {
        String cmdText = "CREATE TABLE IF NOT EXISTS " + Globals.Tables.USER + " ( " +
                            Globals.UserFields.ID + " INT NOT NULL AUTO_INCREMENT PRIMARY KEY," +
                            Globals.UserFields.NAME + " VARCHAR(25) UNIQUE DEFAULT ''," +
                            Globals.UserFields.X + " DOUBLE(9,6) DEFAULT 0.0," +
                            Globals.UserFields.Y + " DOUBLE(9,6) DEFAULT 0.0," +
                            Globals.UserFields.LASTLOGIN + " DATE " +
                            ")";

        MySqlCommand cmd = new MySqlCommand(cmdText, xConnection);

        return Convert.ToBoolean(cmd.ExecuteScalar());
    }
    private static Boolean checkSessionTable(MySqlConnection xConnection)
    {
        String cmdText = "CREATE TABLE IF NOT EXISTS " + Globals.Tables.SESSION + " ( " +
                            Globals.SessionFields.ID + " INT NOT NULL AUTO_INCREMENT PRIMARY KEY," +
                            Globals.SessionFields.DATE + " DATE " +
                            ")";

        MySqlCommand cmd = new MySqlCommand(cmdText, xConnection);

        return Convert.ToBoolean(cmd.ExecuteScalar());
    }
    private static Boolean checkConUsersSessionsTable(MySqlConnection xConnection)
    {
        String cmdText = "CREATE TABLE IF NOT EXISTS " + Globals.Tables.CUSERSESSION + " ( " +
                            Globals.ConUserSessionFields.ID + "id INT NOT NULL AUTO_INCREMENT PRIMARY KEY," +
                            Globals.ConUserSessionFields.USERID + " INT," +
                            Globals.ConUserSessionFields.SESSIONID + " INT," +
                            "Foreign Key (" + Globals.ConUserSessionFields.USERID + ") REFERENCES " + Globals.Tables.USER + " (" + Globals.UserFields.ID + ")," +
                            "Foreign Key (" + Globals.ConUserSessionFields.SESSIONID + ") REFERENCES " + Globals.Tables.SESSION + " (" + Globals.SessionFields.ID + ")" +
                            ")";

        MySqlCommand cmd = new MySqlCommand(cmdText, xConnection);
        return Convert.ToBoolean(cmd.ExecuteScalar());
    }
    #endregion


    public static String[] SplitMessageContent(String pContent)
    {
        return pContent.Split(new string[] { Separator.Content }, StringSplitOptions.None);        
    }
    public static String[] SplitMessage(String pMessage)
    {
        return pMessage.Split(new string[] { Separator.Message }, StringSplitOptions.None);
    }
}


